define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var Message = require("$UI/system/components/justep/common/common");
	var Model = function(){
	   
	    this.isLogin = justep.Bind.observable(localStorage.getItem("userInfo")!=null);
	    this.userName = justep.Bind.observable("");
	    this.imageUrl = justep.Bind.observable("");
	    this.photo = null;
		this.callParent();
	};

	Model.prototype.collectBtnClick = function(event){
		justep.Shell.showPage({url : "$UI/communityOto/mine/houseListActivity.w"});
	};

	Model.prototype.releaseBtnClick = function(event){
		justep.Shell.showPage({url : "$UI/communityOto/mine/houseListActivity.w"});
	};

	Model.prototype.ClickMyInfor = function(event){
		justep.Shell.showPage({url : "$UI/communityOto/mine/myInforActivity.w"});
	};

	Model.prototype.regisBtnClick = function(event){
	      this.owner.setSrc("$UI/communityOto/mine/regisActivity.w");
	      this.owner.refresh();//刷新  		 
	};

	Model.prototype.areaClick = function(event){
	   
		justep.Shell.showPage({url : "$UI/communityOto/mine/areaActivity.w"});
	};

	Model.prototype.loginBtnClick = function(event){
		//this.comp("loginDialog").open();
		this.getParentModel().comp("mineContainer").set("src","$UI/communityOto/mine/login.w");
		this.getParentModel().comp("mineContainer").refresh();
		
	};
	
	 Model.prototype.logined = function(){
		 return this.isLogin.get();
	 };
	
	Model.prototype.logoutBtnClick = function(event){
	
	   
		$(".exitDlg").modal('show');
		
	};
	
	Model.prototype.modelModelConstructDone = function(event){
	    
	    if(localStorage.getItem("userInfo")){
	    	 this.isLogin.set(true);
	         var userInfo = JSON.parse(localStorage.getItem("userInfo"));
	         this.userName.set(userInfo.userName);
	         this.imageUrl.set(this.getImageUrl(userInfo.userImg));    
	    }

	};

	//确认退出按钮
	Model.prototype.messageDialogYes = function(event){
	
	    var userInfo = JSON.parse(localStorage.getItem("userInfo"));
		if(userInfo && userInfo.userType == '4'){
		    $('.MerchantMall').hide();
	    }
	    
		localStorage.removeItem("userInfo");
		localStorage.removeItem("officeInfo");
		localStorage.removeItem("companyInfo");
		this.isLogin.set(false);
		
		//this.owner.set("src","$UI/communityOto/mine/login.w");
		//this.owner.refresh();
		//this.comp("windowReceiver1").windowEnsure();
		
		this.getParentModel().goBackLogin();
		
		//location.reload();
		//justep.Shell.showPage("main");
		
	};
	
	
	
	Model.prototype.changPwdClick = function(event){
		justep.Shell.showPage("$UI/communityOto/mine/changePwd.w");

	};
	
	Model.prototype.getImageUrl= function(path){
	    var localPath = require.toUrl("$UI/communityOto/img/user_photo_default.png");
		var imageUrl = path?"http://www.xjzlhy.com:81"+path:localPath;
		return imageUrl;
	};
	
	
	Model.prototype.logoutClick = function(event){
		var userInfo = JSON.parse(localStorage.getItem("userInfo"));
		if(userInfo.userType == '4'){
		    $('.MerchantMall').hide();
	    }
	    
		localStorage.removeItem("userInfo");
		localStorage.removeItem("officeInfo");
		localStorage.removeItem("companyInfo");
		this.isLogin.set(false);
	
		
		this.getParentModel().goBackLogin();
	};
	
	
	Model.prototype.exitClick = function(event){
		navigator.app.exitApp();
	};
	
	
	
	//更换头像
	Model.prototype.photoChange = function(event){
	
	    
	    this.photo = event.currentTarget.files[0];
		this.comp("messageDialog1").show();

	};
	
	
	
	
	Model.prototype.messageDialog1Yes = function(event){
		//$(".my-photo").attr("src",window.URL.createObjectURL(this.photo));
		var  formData = new FormData();
		
		var userId = JSON.parse(localStorage.getItem('userInfo')).userId;
		formData.append('id',userId);
		formData.append('image',this.photo);
		var me = this;
		
		var changePhotoUrl= "http://www.xjzlhy.com:81/ex_community/a/api/community/updatePhoto?";
		$.ajax({
				  url: changePhotoUrl,
				  type: "POST",
				  data: formData,
				  processData: false,  // 告诉jQuery不要去处理发送的数据
				  contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
				  success:function(data){
				      
		  				justep.Util.hint("更换头像成功", {type:"success"});
		  				//console.log(me.getImageUrl(data.data[0].userImg));
		  				
		  				//$(".my-photo").attr('src',me.getImageUrl(data.data[0].userImg));
		  				me.imageUrl.set(me.getImageUrl(data.data[0].userImg));
		  				
		  				//更新localStory中的头像数据
		  				
		  				localStorage.setItem('userInfo',JSON.stringify(data));
			      }
				});
			
	};
	
	
	return Model;
});